package ReefStats::Controller::Clusters;

use strict;
use warnings;
use base 'Catalyst::Controller::FormBuilder';

sub index : Private {
    my ( $self, $c ) = @_;

    $c->forward('/clusters/list');
}

sub list : Local {
    my ( $self, $c ) = @_;

    $c->stash->{cluster_list} =
      [ $c->model('Clusters')->search( undef, { order_by => "cluster" } ) ];
    $c->stash->{template} = "clusters/list.tt2";
}

sub edit : Local Form Args(1) {
    my ( $self, $c, $cluster_id ) = @_;

    if ( ( defined $cluster_id ) and ( $cluster_id !~ /^\d+$/ ) ) {
        $c->flash->{error} = 'Invalid cluster identifier provided';
        $c->response->redirect( $c->uri_for('list') );
        $c->detach();
    }

    my $cluster = $c->model('Clusters')->find_or_new( { id => $cluster_id } );
    $c->stash->{cluster} = $cluster;

    if ( $self->formbuilder->submitted && $self->formbuilder->validate ) {
        $cluster->alias( $self->formbuilder->field('alias') );
        $cluster->visible( $self->formbuilder->field('visible') );
        $cluster->update_or_insert;
        $c->flash->{message} = 'Updated cluster ' . $cluster->cluster;
        $c->response->redirect( $c->uri_for('list') );
        $c->detach();
    }

    $self->formbuilder->field( name => 'alias', value => $cluster->alias );
    $self->formbuilder->field(
        name  => 'visible',
        value => $cluster->visible
    );
}

1;

__END__

=head1 NAME

ReefStats::Controller::Clusters - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 index 

=cut


=head1 AUTHOR

,,,

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

